package com.hutou.chuangke.fragmentnet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.hutou.chuangke.R;
import com.hutou.chuangke.fragment.AttachFragment;
import com.hutou.chuangke.fragment.FolderFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tyhdark on 2017/3/18.
 * 动态fragment
 */
public class TrackFragment extends AttachFragment{
    private ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_track,container,false);
        viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
        if(viewPager != null){
            setupViewPager(viewPager);
            viewPager.setOffscreenPageLimit(3);
        }

        final TabLayout tableLayout = (TabLayout) rootview.findViewById(R.id.tabs);
        tableLayout.setTabTextColors(R.color.text_color, ThemeUtils.getThemeColorStateList(mContext, R.color.theme_color_primary).getDefaultColor());
        tableLayout.setSelectedTabIndicatorColor(ThemeUtils.getThemeColorStateList(mContext, R.color.theme_color_primary).getDefaultColor());
        tableLayout.setupWithViewPager(viewPager);
        return rootview;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void setupViewPager(ViewPager viewPager){
        Adapter adapter = new Adapter(getChildFragmentManager());
        adapter.addFragment(new TrackListFragment(),getString(R.string.track));
        adapter.addFragment(new TrackListFragment(),getString(R.string.nearby));
        adapter.addFragment(new FriendsListFragment(),getString(R.string.friend));
        adapter.addFragment(new ActivityFragment(),getString(R.string.activity));
        viewPager.setAdapter(adapter);
    }

    static class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }
}
